﻿using Autofac;
using SYSoft.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace SYSoft.ServiceExtensions
{
    public class AutofacModuleSetUp :Autofac.Module
    {

        protected override void Load(ContainerBuilder builder)
        {
            #region 带有接口层的类 注入

            var servicesDllFile = Path.Combine(AppContext.BaseDirectory, "SYSoft.Services.dll");
            var repositoryDllFile = Path.Combine(AppContext.BaseDirectory, "SYSoft.Repository.dll");


            builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency();//注册泛型仓储

            // 获取 Service.dll 程序集服务，并注册
            var assemblysServices = Assembly.LoadFrom(servicesDllFile);
            builder.RegisterAssemblyTypes(assemblysServices)
                      .AsImplementedInterfaces()
                      .InstancePerDependency();

            // 获取 Repository.dll 程序集服务，并注册
            var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
            builder.RegisterAssemblyTypes(assemblysRepository)
                   .AsImplementedInterfaces()
                   .InstancePerDependency();
            #endregion
        }


    }
}
